home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 287_01 / ellipse.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-05-23  |  6.8 KB  |  230 lines

  1. #include <stdio.h>
  2. #include <gds.h>
  3.  
  4. extern ARCSTTX, ARCSTTY, ARCENDX, ARCENDY, ARCSTTR, ARCENDR;
  5.  
  6. struct cpoint {
  7.     int point, state, region;
  8. };
  9.  
  10. /*===========================================================*
  11.  *                                                           *
  12.  *  This file contains 3 external usable routines to draw    *
  13.  *  ellipse and arc. (Ell is intended to call by Earc2 only) *
  14.  *      Ellipse(centerx,centery,a,b)                         *
  15.  *          It draws a ellipse with center at                *
  16.  *          (centerx,centery) and length of major and minor  *
  17.  *          axes equal to a and b respectively. If a and b   *
  18.  *          are equal, it becomes the radius of a logical    *
  19.  *          circle with center at (centerx,centery). But     *
  20.  *          sometimes when an ellipse appear on an asymmet-  *
  21.  *          ical display, it may look like a circle more     *
  22.  *          than the circle draw by the Circle command.      *
  23.  *                                                           *
  24.  *      EARC1(centerx,centery,a,b,spec)                      *
  25.  *          Draw n/8 of an ellipse. n can be 0 to 8. Read    *
  26.  *          user's manual on the use of the function.        *
  27.  *                                                           *
  28.  *===========================================================*/
  29.  
  30. Ellipse(ctrx,ctry,a,b)
  31. int ctrx,ctry,a,b;
  32. {
  33.     register int tmp1,tmp2;
  34.     struct cpoint dummy;
  35.  
  36.     if ((a<=0) || (b<=0)) return;
  37.     ctrx+=ORGX;   /* get absolute position */
  38.     ctry+=ORGY;
  39.  
  40.     /* return if the whole ellipse is outside the window */
  41.     if ((ctrx+a < WINX1) || (ctrx-a > WINX2) ||
  42.         (ctry+b < WINY1) || (ctry-b > WINY2)) return;
  43.     tmp1=ctrx+a; tmp2=ctry+b;
  44.     if (outside(tmp1,tmp2)) {
  45.         seteptr(1);    /* use '1' if part of the ellipse may be outside
  46.                           the window */
  47.     } else {
  48.         tmp1=ctrx-a; tmp2=ctry-b;
  49.         if (outside(tmp1,tmp2))
  50.             seteptr(1);   /* use '1' if part of the ellipse may be outside
  51.                              the window */
  52.         else 
  53.             seteptr(0);
  54.     }
  55.     /* draw the whole circle and use solid line */
  56.     dummy.point=32767;
  57.     dummy.region=0x8000;
  58.     Ell(ctrx,ctry,a,b,0xff,0xffff,&dummy,&dummy,&dummy,&dummy); 
  59. }
  60.  
  61. Earc1(ctrx,ctry,a,b,spec)
  62. int ctrx,ctry,a,b,spec;
  63. {
  64.     struct cpoint dummy;
  65.  
  66.     if ((a<=0) || (b<=0)) return;
  67.     ctrx+=ORGX;   /* get absolute position */
  68.     ctry+=ORGY;
  69.  
  70.     /* return if the whole ellipse is outside the window */
  71.     if ((ctrx+a < WINX1) || (ctrx-a > WINX2) ||
  72.         (ctry+b < WINY1) || (ctry-b > WINY2)) return;
  73.     seteptr(1);
  74.     dummy.point=32767;
  75.     dummy.region=0x8000;
  76.     Ell(ctrx,ctry,a,b,spec,STYLE,&dummy,&dummy,&dummy,&dummy);
  77. }    
  78.  
  79.  
  80. /*-----------------------------------------------------------*
  81.  *  The method used in drawing ellipse is similar to drawing *
  82.  *  circle. However, it is not described in Foley[1982] and  *
  83.  *  it is dervied by myself. The result is shown in the      *
  84.  *  system manual.                                           *
  85.  *-----------------------------------------------------------*/
  86. Ell(ctrx,ctry,a,b,spec,sty,pnt1,pnt2,pnt3,pnt4)
  87. int ctrx,ctry,a,b,spec,sty;
  88. struct cpoint *pnt1, *pnt2, *pnt3, *pnt4;
  89. {
  90.     extern int cirsty;
  91.     long incr1,incr2,a2,b2,d,step1,step2,test,a_b,step3;
  92.     register int x,y;
  93.     int temp, oldx, oldy;
  94.  
  95.     ARCSPEC=spec & 0x55;
  96.     cirsty=sty;
  97.     x=0;
  98.     y=b;
  99.     a_b = (a2= (long) a * a) + (b2= (long) b * b);
  100.     step3 = (step1 = a2 << 2) + (step2 = b2 << 2);
  101.     d= ((b2 - (test=a2 * b)) << 1) + a2;
  102.     incr1= step2 + (b2 << 1);
  103.     incr2= incr1 + step1 - (test << 1);
  104.     incr2 -= test << 1;
  105.     wrtepta(x++,y,ctrx,ctry); 
  106.     ARCSPEC=spec;
  107.     goto normal1;
  108.     /* Begining of the first main loop */
  109.     while (test > 0) {
  110.         if (!pnt1->point--) {
  111.             spec |= pnt1->region;
  112.             if (pnt1->state) {
  113.                 spec ^= pnt1->region;
  114.                 ARCSTTX=oldx;
  115.                 ARCSTTY=oldy;
  116.             } else {
  117.                 ARCSTTX=x;
  118.                 ARCSTTY=y;
  119.             }
  120.             ARCSPEC=spec;
  121.         }
  122.         if (!pnt2->point--) {
  123.             spec |= pnt2->region;
  124.             if (pnt2->state) {
  125.                 spec ^= pnt2->region;
  126.                 ARCENDX=oldx;
  127.                 ARCENDY=oldy;
  128.             } else {
  129.                 ARCENDX=x;
  130.                 ARCENDY=y;
  131.             }
  132.            ARCSPEC=spec;
  133.         }
  134.         wrtept(x++,y,ctrx,ctry);
  135. normal1:
  136.         oldx=x;
  137.         oldy=y;
  138.         if (d<0) {
  139.             d+=incr1;
  140.             incr2+=step2;
  141.             test-=b2;
  142.         }
  143.         else {
  144.             d+=incr2;
  145.             incr2+=step3;
  146.             y--;
  147.             test-=a_b;
  148.         }
  149.         incr1+=step2;
  150.     }
  151.     if (test == 0) {
  152.         ARCSPEC=spec & 0xaa;
  153.         wrtepta(x,y,ctrx,ctry);
  154.         LASTX2=x;
  155.         LASTY2=y;
  156.     } else {
  157.         LASTX2=oldx;
  158.         LASTY2=oldy;
  159.     }
  160.     spec=((spec << 1) & 0x88) | ((spec >> 1) & 0x11);
  161.     ARCSPEC=spec & 0xaa;
  162.     cirsty=sty;
  163.     y=0;
  164.     x=a;
  165.     d= ((a2 - (test=b2 * a)) << 1) + b2;
  166.     incr1= step1 + (a2 << 1);
  167.     incr2= incr1 + step2 - (test << 1);
  168.     incr2 -= test << 1;
  169.     wrtepta(x,y++,ctrx,ctry); 
  170.     ARCSPEC=spec;
  171.     goto normal2;
  172.     /* Begining of the second main loop */
  173.     while (test > 0) {
  174.         if (!pnt3->point--) {
  175.             temp=pnt3->region;
  176.             temp=((temp<<1) & 0x88) | ((temp >> 1) & 0x11);
  177.             spec |= temp;
  178.             if (pnt3->state) {
  179.                 spec ^= temp;
  180.                 ARCSTTX=oldy;
  181.                 ARCSTTY=oldx;
  182.             } else {
  183.                 ARCSTTX=y;
  184.                 ARCSTTY=x;
  185.             }
  186.             ARCSPEC=spec;
  187.         }
  188.         if (!pnt4->point--) {
  189.             temp=pnt4->region;
  190.             temp=((temp<<1) & 0x88) | ((temp >> 1) & 0x11);
  191.             spec |= temp;
  192.             if (pnt4->state) {
  193.                 spec ^= temp;
  194.                 ARCENDX=oldy;
  195.                 ARCENDY=oldx;
  196.             } else {
  197.                 ARCENDX=y;
  198.                 ARCENDY=x;
  199.             }
  200.             ARCSPEC=spec;
  201.         }
  202.         wrtept(x,y++,ctrx,ctry);
  203. normal2:
  204.         oldx=x;
  205.         oldy=y;
  206.         if (d<0) {
  207.             d+=incr1;
  208.             incr2+=step1;
  209.             test-=a2;
  210.         }
  211.         else {
  212.             d+=incr2;
  213.             incr2+=step3;
  214.             x--;
  215.             test-=a_b;
  216.         }
  217.         incr1+=step1;
  218.     }
  219.     if (test==0) {
  220.         ARCSPEC=spec & 0x55;
  221.         wrtepta(x,y,ctrx,ctry);
  222.         LASTX=y;
  223.         LASTY=x;
  224.     } else {
  225.         LASTX=oldy;
  226.         LASTY=oldx;
  227.     }
  228. }
  229.  
  230.